<?php
if (!defined("MCTDESIGN")) die("# ");
// xajax

if (isset($_POST['delete'])) {
	$total = isset($_POST["total"]) ? intval($_POST["total"]) : 0;
 	for($i=1;$i<=$total;$i++){
		$checkbox[$i] = isset($_POST["checkbox_$i"]) ? $_POST["checkbox_$i"] : '';
		if (!empty($checkbox[$i])) {
            $dhaid = $checkbox[$i];
            $mysql->query("DELETE FROM ".$tb_fix."_customer WHERE cus_id=(select cus_id from ".$tb_fix."_don_hang where dha_id = $dhaid)");
			$mysql->query("DELETE FROM ".$tb_fix."_don_hang WHERE dha_id='$dhaid'");
            $mysql->query("DELETE FROM ".$tb_fix."_thanh_toan_dh WHERE dha_id='$dhaid'");
            $mysql->query("DELETE FROM ".$tb_fix."_chi_tiet_dh WHERE dha_id='$dhaid'");
		}
	}
	$html_body = _html_trans("Đã xoá đơn hàng thành công !");
	return false;
}
$_c = 0;
function show_managerCategory($parent="0", $space=0){
	global $mysql, $tb_fix, $_c;
    $_limit = 15;
    $page = isset($_GET["page"]) ? intval($_GET["page"]) : 1;
	$q = $mysql->query("SELECT d.*,c.cus_fullname FROM ".$tb_fix."_don_hang AS d
                        JOIN ".$tb_fix."_customer AS c
                        ON d.cus_id = c.cus_id    
                        ORDER BY d.dha_id DESC");
    $t = $mysql->num_rows($q);
    if($t>0){
            $_start = ($page-1) * $_limit ;
			$_end   = $page*$_limit;
			$num_pages = ceil($t/$_limit) ;
			if ($page > $num_pages) $page=$num_pages;
			if ($page < 1 ) $page=1;
			$page_view = Paginate_Admin($t, $_limit, $page, "page");
			$i = 0;
            $html="";
    	while ($r = $mysql->fetch_array($q)) {
    	   if ($i>=$_start) {
    			$_c++;
    			
                //$ngaydat = gmdate("d/m/Y H:i", $r["dha_ngay_tao"] + 7*3600);
                $ngaydat = gmdates($r["dha_ngay_tao"]);
                $madh = code_donhang().$r["dha_ten_dh"];
                $tongtien = number_format($r["dha_tong_tien"], 0, '.', '.');
                if($r['dha_da_xem'] == 1) $daxem = "style='color: #cccccc'"; else $daxem='';
                $html .= "<tr class=\"row1\" id='tr_dha_".$r["dha_id"]."'>";
                $html .= "<td align=\"center\" width=\"5%\" style=\"text-align:center;\"><input type='checkbox' id='checkbox' name='checkbox_".$_c."' value='".$r["dha_id"]."'></td>";                
    			$html .= "<td align=\"center\" width=\"32%\" id=\"manu_".$r["dha_id"]."\"><div style=\"margin-left:5px;\"><a href='?obj=DonHang&act=view&id=".$r["dha_id"]."' $daxem><b>$madh</b></a></div></td>";
                $html .= "<td style='text-align: center;color: red' align=\"center\" width=\"15%\" id=\"manu_".$r["dha_id"]."\">$tongtien</td>";
    			$html .= "<td style='text-align: center' width=\"17%\" align=\"left\" >".$r["cus_fullname"]."</td>";
                $html .= "<td style='text-align: center' width=\"18%\" align=\"left\" >$ngaydat</td>";
    			$html .= "<td width=\"13%\" id=\"adm_action\">
                        <div id='status_".$r["dha_id"]."' style='float: left;cursor: pointer;padding-top: 9px;'>".show_img_status($r["dha_status"],$r["dha_id"])."</div>
                        <a href=\"?obj=DonHang&act=view&id=".$r["dha_id"]."\" title=\"Sửa đơn hàng\" class=\"btn_no_text btn ui-state-default ui-corner-all tooltip\"> 
                            <span class=\"ui-icon ui-icon-wrench\"></span>
                        </a>
                        <a title=\"Xóa đơn hàng\" href=\"javascript:;\" onclick=\"check_del_row(".$r["dha_id"].")\" class=\"btn_no_text btn ui-state-default ui-corner-all tooltip\"> 
                           <span class=\"ui-icon ui-icon-circle-close\"></span>
                        </a>
                        </td>";
    			$html .= "</tr>";
          }
          $i++;
	       if ($i>=$_end) break;      
    	}
    }
	return array($html,$page_view);
}

function _html_manageCategory()
{
	global $dir_tpl, $mysql, $tb_fix,$admin_id,$admin_permissions,$_c,$dir_tpl,$temp;
	$temp_body = $temp;
	list($list_donhang,$page_view) = show_managerCategory($parent="0", $space="");
	$list_donhang .= "<input type='hidden' name='total' value='".$_c."'>";
	$list_donhang .= "<tr class=\"row1\">";
	$list_donhang .= "<td align=\"center\" colspan=\"6\"><input class=\"btn_no_text btn ui-state-default ui-corner-all tooltip\" name='delete' type='submit' value=' Xoá ' onClick=\"return check_checkbox();\"></td>";
	$list_donhang .= "</tr>";
	$temp_body->assign( 'list_donhang', "".$list_donhang."" );
    $temp_body->assign( 'page_view', "".$page_view."" );
    $temp_body->assign('dir_tpl',$dir_tpl);
    $temp_body->assign('breakcum',breakcum("Quản lý đơn hàng"));
	$_html = $temp_body->fetch( '_adm_donhang.mct' );
	return $_html;
}
function show_product_in_order($dha_id){
    global $mysql, $tb_fix;
    $sql = "select p.pro_name,p.pro_id,p.pro_promotion_vnd,c.ctd_so_luong,c.ctd_tong_tien 
            from ".$tb_fix."_product as p
            join ".$tb_fix."_chi_tiet_dh as c
            on c.pro_id = p.pro_id
            where c.dha_id = $dha_id";
    $q = $mysql->query($sql);
    $t = $mysql->num_rows($q);
    $html ="";
    if($t>0){
        while($r = $mysql->fetch_array($q)){
            $giaban = number_format($r["pro_promotion_vnd"], 0, '.', '.');
            $thanhtien = number_format($r["ctd_tong_tien"], 0, '.', '.');
            $html .= "<tr class=\"row1\" align=\"left\" valign=\"top\"> ";
			$html .= "		<td style=\"text-align: center;\">".$r['pro_name']."</td> ";
			$html .= "		<td style=\"text-align: center;\">".$r['ctd_so_luong']."</td> ";
			$html .= "		<td style=\"text-align: center;\">$giaban</td> ";
			$html .= "		<td style=\"text-align: left;color: red;\">$thanhtien</td> ";
			$html .= "</tr> ";
        }
    }else{
        $html .= "<tr class=\"row1\" align=\"left\" valign=\"top\"> ";
        $html .= "		<td style=\"text-align: left;\" colspan=\"4\">Không có sản phẩm trong đơn đặt hàng này! </td> ";
        $html .= "</tr>";
    }
    return $html;
            
}
function _html_viewDonHang($_id="")

{
	global $dir_tpl, $mysql, $tb_fix,$admin_id,$admin_permissions,$temp;
    $sql = "select d.*,cu.cus_cmnd,cu.cus_email,cu.cus_address,cu.cus_tel,cu.cus_fullname,t.* 
            from ".$tb_fix."_don_hang as d
            join ".$tb_fix."_chi_tiet_dh as c
                on c.dha_id = d.dha_id
            join ".$tb_fix."_thanh_toan_dh as t
                on t.dha_id = d.dha_id
            join ".$tb_fix."_customer as cu
                on cu.cus_id = d.cus_id
            join ".$tb_fix."_product as p
                on p.pro_id = c.pro_id
            where d.dha_id = $_id";
    $q = $mysql->query($sql);        

	$t = $mysql->num_rows($q);

	if ($t>0) {

		$r = $mysql->fetch_array($q);
        // update da xem
        update_view_dh($r['dha_id']);
		$temp_body = $temp;
        if($r['dha_status'] == 0) {
            $dha_status = '<font color="red">Chưa xử lý</font>';
        }
        else {
            $dha_status = '<font color="green">Đã xử lý</font>';
        };
        $imgSubmit = "";
        if($r['dha_status'] == 0){
            $imgSubmit .= "<input type=\"button\" value=\"Hoàn thành\" name=\"add\" class=\"ui-state-default ui-corner-all\" onclick=\"xajax_change_status_dh($r[dha_id],1,'view')\">";
        }
		$temp_body->assign(array(

								"dh_name" => code_donhang()."".$r["dha_ten_dh"]."",
								"dh_tongtien" => number_format($r["dha_tong_tien"], 0, '.', '.'),
								"dh_ngaytao" => gmdates($r["dha_ngay_tao"]),
                                "dh_status"  => "$dha_status",
                                "dh_list_sp" => show_product_in_order($r['dha_id']),
                                "ttd_hinh_thuc_tt" => $r["ttd_hinh_thuc_tt"],
                                "ttd_dia_chi_nh" => $r["ttd_dia_chi_nh"],
                                "cus_cmnd" => $r["cus_cmnd"],
                                "cus_email" => $r["cus_email"],
                                "cus_address" => $r["cus_address"],
                                "cus_tel" => $r["cus_tel"],
                                "cus_fullname" => $r["cus_fullname"],
                                "ttd_ghi_chu" => $r["ttd_ghi_chu"],
                                "dha_status_bt" => $imgSubmit
							)
						);
        $temp_body->assign("breakcum",breakcum("Xem đơn hàng"));
		$_html = $temp_body->fetch( '_adm_donhang_view.mct' );

	}

	return $_html;

}
function update_view_dh($dhaid){
    global $tb_fix,$mysql;
    $sql = "select dha_da_xem from ".$tb_fix."_don_hang where dha_id = $dhaid";
    $r = $mysql->fetch_array($mysql->query($sql));
    $has_viewed = $r['dha_da_xem'];
    if($has_viewed == 0){
       $sql = "update ".$tb_fix."_don_hang set dha_da_xem = 1 where dha_id = $dhaid";
       $mysql->query($sql); 
    }
    return true;
}
if ($act=="view") {

	$html_body = _html_viewDonHang($id);

	return false;

}

$html_body = _html_manageCategory();
?>